import pika


def connect_rabbitmq(host, port, username, password):
    credentials = pika.PlainCredentials(username, password)
    parameters = pika.ConnectionParameters(host=host, port=port, credentials=credentials)
    connection = pika.BlockingConnection(parameters)
    channel = connection.channel()
    return connection, channel


def publish_message(exchange, routing_key, message):
    connection, channel = connect_rabbitmq("localhost", 5672, "guest", "guest")
    channel.exchange_declare(exchange=exchange, exchange_type="direct")
    channel.basic_publish(
        exchange=exchange,
        routing_key=routing_key,
        body=message,
        properties=pika.BasicProperties(content_type="text/plain", delivery_mode=2)
    )
    connection.close()


def consume_messages(queue, callback):
    connection, channel = connect_rabbitmq("localhost", 5672, "guest", "guest")
    channel.queue_declare(queue=queue, durable=True)
    channel.basic_consume(queue=queue, on_message_callback=callback, auto_ack=True)
    channel.start_consuming()
    connection.close()
